﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarInfor.DAL;

namespace CarInfor.UI
{
    public partial class frmInsuranceItemSet : Form
    {
        public frmInsuranceItemSet()
        {
            InitializeComponent();
            Bind();
        }

        private void Bind()
        {
            InsuranceItem it = new InsuranceItem();
            this.dataGridView1.DataSource = it.Query();
            this.dataGridView1.Columns["编号"].Visible = false;
            this.dataGridView1.Columns["险种名称"].Width = 100;
            this.dataGridView1.Columns["保额"].Width = 100;
            this.dataGridView1.Columns["金额"].Width = 100;
            this.dataGridView1.Columns["保额"].DefaultCellStyle.Format = "0.00";
            this.dataGridView1.Columns["保额"].DefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight;
            this.dataGridView1.Columns["金额"].DefaultCellStyle.Format = "0.00";
            this.dataGridView1.Columns["金额"].DefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight;
            foreach (DataGridViewColumn col in this.dataGridView1.Columns)
            {
                col.SortMode = DataGridViewColumnSortMode.NotSortable;
            }
            this.dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

        }

        /// <summary>
        /// 添加保险项目
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmInsuranceItemEdit updateitem = new frmInsuranceItemEdit(string.Empty, ActionType.New);
            if (updateitem.ShowDialog() == DialogResult.OK)
            {
                Bind();
            }
        }

        /// <summary>
        /// 编辑保险项目
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUpate_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count != 0)
            {
                string id = this.dataGridView1.SelectedRows[0].Cells["编号"].Value.ToString();
                frmInsuranceItemEdit updateitem = new frmInsuranceItemEdit(id, ActionType.Edit);
                if (updateitem.ShowDialog() == DialogResult.OK)
                {
                    Bind();
                }
            }
        }

        private void btnColse_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count != 0)
            {
                if (MessageHelper.ShowYesNoChoice("确定删除：'" + this.dataGridView1.SelectedRows[0].Cells["险种名称"].Value.ToString() + "'吗?") == DialogResult.Yes)
                {
                    try
                    {
                        InsuranceItem it = new InsuranceItem();
                        it.Delete(this.dataGridView1.SelectedRows[0].Cells["编号"].Value.ToString());
                        Bind();
                    }
                    catch
                    {
                        MessageHelper.ShowFail("删除失败！");
                    }

                }
            }
        }
    }
}
